<template>
	<view class="container">
		<!-- 收件人/手机号码 -->
		<view class="man_phone">
			<view class="left">
				<view>
					收件人:  <input class="uni-input" v-model="addressData.receive_name" type="text" placeholder="请输入收件人姓名" />
				</view>
				<view>
					手机号码:  <input class="uni-input" v-model="addressData.receive_tel" type="number" placeholder="请输入收件人联系方式" />
				</view>
			</view>
			<!-- <view class="right">
				<image src="https://community.chuangxiangdianli.com/images/me/add_man.png" mode=""></image>
			</view> -->
		</view>
		<!-- 所在地区 -->
		<view class="address" @click="areasFlag = !areasFlag">
			<view class="szdq">所在地区:</view>
			<view class="djxz" v-if="!sheng && !shi && !qu && !area_info">
				<image src="https://community.chuangxiangdianli.com/images/me/add_add.png" mode=""></image>
				点击选择
			</view>
			<view class="djxz" v-if="(sheng && shi && qu) || area_info">
				<text v-if="area_info">{{ area_info }}</text>
				 <text v-if="sheng && shi && qu">{{ sheng }}-{{ shi }}-{{ qu }}</text>
			</view>
			<view class="jt" v-if="!sheng && !shi && !qu && !area_info">
				<image src="https://community.chuangxiangdianli.com/images/me/my_arrow.png" mode=""></image>
			</view>
		</view>
		<!-- 详细地址 -->
		<view class="addressDetail">
			<view>详细地址:</view>
			<textarea v-model="addressData.address" value="" placeholder="楼牌号，宿舍房间号等详细地址" />
		</view>
		<!--  设置默认地址 -->
		<view class="default">
			<view class="left">
				<view>设置默认地址:</view>
				<view>提醒：每次下单时会默认使用该地址，请在下单时确认！</view>
			</view>
			<view class="right" @click="changeDefault">
				<image v-if="addressData.is_default === 0" src="https://community.chuangxiangdianli.com/images/me/add_one.png" mode=""></image>
				<image v-if="addressData.is_default === 1" src="https://community.chuangxiangdianli.com/images/me/add_two.png" mode=""></image>
			</view>
		</view>
		<!-- 按钮 -->
		<view class="btn" @click="submit">
			确定
		</view>
		<!--  地区选择 -->
		<u-picker mode="region" @confirm="confirm" @cancel="areasFlag = false" v-model="areasFlag" :area-code='["13", "1303", "130304"]'></u-picker>
	</view>
</template>

<script>
	export default{
		//  ===============================省市区联动组件  参考生养app===============================
		data() {
			return {
				address_id: '',
				areasFlag: false,  // 是否显示地区选择
				addressData: {
					receive_name: '', // 收货人姓名
					receive_tel: '', // 收货人电话
					province_id: '', // 省份id
					city_id: '', // 城市id、
					region_id: '', // 区县id
					is_default: 1, // 是否默认
					address: '', // 详细地址
				},
				areas: [], // 地区数据
				sheng: '',
				shi: '',
				qu: '',
				area_info: '',
				arr: '', // 结算页面数据
			}
		},
		onLoad(option) {
			if(option.id){
				this.address_id = option.id
			}
			if (option.arr) {
				this.arr = option.arr
			}
			this.getAreas()
		},
		onShow() {
			if (this.address_id) {
				this.getDetailById()
			}
		},
		methods: {
			//  根据id查询数据
			getDetailById() {
				var that = this
				this.$http.post({
					url: 'Mini/address/getAddress',
					data: {
						address_id: that.address_id
					}
				}).then(res => {
					if (res.status) {
						that.addressData.address_id = res.data[0].id
						that.addressData.receive_name = res.data[0].receive_name
						that.addressData.receive_tel = res.data[0].receive_tel
						that.addressData.province_id = res.data[0].province_id
						that.addressData.city_id = res.data[0].city_id
						that.addressData.region_id = res.data[0].region_id
						that.addressData.address = res.data[0].address
						that.area_info = res.data[0].area_info
						that.addressData.is_default = res.data[0].is_default
						
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				})
			},
			//  点击确定选择地区
			confirm(data) {
				var that = this
				this.areas.forEach((item, index) => {
					if (item.name == data.province.label) { 
						that.addressData.province_id = item.id
						that.sheng = data.province.label
						item.children.forEach((item1, index1) => {
							if (item1.name == data.city.label) {
								that.shi = data.city.label
								that.addressData.city_id = item1.id
								item1.children.forEach((item2, index2) => {
									if (item2.name == data.area.label) {
										that.addressData.region_id = item2.id
										that.qu = data.area.label
									}
								})
							}
						})
					}
				})
			},
			//  点击确定提交数据
			submit() {
				if (!this.addressData.receive_name) {
					uni.showToast({
						title: '请输入收货人姓名’',
						icon: 'none'
					})
					return 
				}
				if (!this.addressData.receive_tel) {
					uni.showToast({
						title: '请输入收货人电话’',
						icon: 'none'
					})
					return 
				}
				if (!this.addressData.address) {
					uni.showToast({
						title: '请输入收货详细地址’',
						icon: 'none'
					})
					return 
				}
				if (!this.addressData.region_id || !this.addressData.province_id || !this.addressData.city_id) {
					uni.showToast({
						title: '请完善所在地区’',
						icon: 'none'
					})
					return 
				}
				var that = this
				that.addressData.token = 'Bearer' + ' ' + uni.getStorageSync("userInfo").token
				//  判断是编辑还是新增
				if (that.address_id) {
					this.$http.post({
						url: 'Mini/address/edit',
						data: that.addressData
					}).then(res => {
						if (res.status) {
							uni.showToast({
								title: '修改成功！',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						}
					}).catch(res => {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					})
				} else {
					this.$http.post({
						url: 'Mini/address/add',
						data: that.addressData
					}).then(res => {
						if (res.status) {
							uni.showToast({
								title: '新增成功！',
								icon: 'none'
							})
							if (that.arr) {
								setTimeout(() => {
									uni.navigateTo({
										url: '/other/settlement/index?arr=' + that.arr
									})
								}, 1000)
							} else {
								setTimeout(() => {
									uni.navigateBack()
								}, 1000)
							}
						}
					}).catch(res => {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					})
				}
				
			},
			// 更改是否是默认地址
			changeDefault() {
				this.addressData.is_default === 1 ? this.addressData.is_default = 0 : this.addressData.is_default = 1;
				// console.log(this.addressData.is_default)
			},
			// 获取地区数据
			getAreas() {
				var that = this
				uni.request({
					url: 'https://community.chuangxiangdianli.com/api/areas',
					header: {
						Authorization: 'Bearer' + ' ' + uni.getStorageSync("userInfo").token
					},
					success(res) {
						if(res.data.code == 200){
							that.areas = res.data.data
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					fail(res) {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		border-top: 2rpx solid rgba(51, 51, 51, .1);
	}
	.man_phone{
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
		.left{
			flex: 1;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
			&>view{
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				align-items: center;
				height: 88rpx;
				&>input{
					margin-left: 20rpx;
				}
			}
			&>view:nth-child(2){
				border-top: 2rpx solid rgba(51, 51, 51, .1);
			}
			
		}
		.right{
			width: 119rpx;
			height: 86rpx;
			margin-left: 30rpx;
			margin-right: 30rpx;
			&>image{
				display: block;
				height: 100%;
				width: 100%;
			}
		}
	}
	.address{
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		height: 86rpx;
		margin: 0 30rpx;
		border-top: 2rpx solid rgba(51, 51, 51, .1);
		&>view{
			line-height: 86rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
		}
		.djxz{
			flex: 1;
			color: #999999;
			margin-left: 30rpx;
			&>image{
				display: inline-block;
				height: 32rpx;
				width: 28rpx;
				margin-right: 16rpx;
			}
		}
		.jt{
			width: 20rpx;
			height: 22rpx;
			&>image{
				display: block;
				width: 14rpx;
				height: 22rpx;
			}
		}
	}
	.addressDetail{
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		// line-height: 86rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #333333;
		margin: 0 30rpx;
		padding-top: 20rpx;
		box-sizing: border-box;
		border-top: 2rpx solid rgba(51, 51, 51, .1);
		border-bottom: 2rpx solid rgba(51, 51, 51, .1);
		&>textarea{
			flex: 1;
			height: 120rpx;
			margin-left: 30rpx;
		}
		// /deep/ .inputStyle{
		// 	line-height: 86rpx;
		// 	font-size: 24rpx;
		// 	font-family: PingFang SC;
		// 	font-weight: 400;
		// 	color: #333333;
		// }
	}
	.default{
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		margin: 24rpx 20rpx 0;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #333333;
		.left{
			flex: 1;
		}
		.left>view:nth-child(2){
			font-size: 22rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
			opacity: .5;
		}
		.right{
			width: 70rpx;
			height: 43rpx;
			&>image{
				display: block;
				width: 100%;
				height: 100%;
			}
		}
	}
	.btn{
		height: 90rpx;
		margin: 150rpx 30rpx 0rpx;
		background: linear-gradient(-74deg, #70DAC4, #3EC4A7);
		border-radius: 45rpx;
		line-height: 90rpx;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
	}
</style>
